<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");

$title = checkTrim($_POST['title']);
$code = checkTrim($_POST['code']);
$event = checkTrim($_POST['event']);
$gold = checkTrim($_POST['gold']);
$level = checkTrim($_POST['level']);
$isModify = $_POST['isModify'];

$db = new DBC();
$is_code_exist = false;
if ($isModify == 1) {
    $sql = "UPDATE t_achieve_info SET title = '" . $title . "',event = '" . $event . "',gold = '" . $gold . "',level = '" . $level . "' WHERE code = '" . $code . "'";
} else {
    $sql = "SELECT id FROM t_achieve_info WHERE code = '" . $code . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    $sql = "INSERT INTO t_achieve_info(code,title,event,gold,level) VALUES('" . $code . "','" . $title . "','" . $event . "','" . $gold . "','".$level."')";
}
if ($is_code_exist) {
    $msg = "编号 " . $code . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    $transfer = "achieveList.php?menu=achieve";
    if ($db->execute($sql)) {
        //success
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
